import * as Cesium from 'cesium';
function Overlay(viewer) {
    this.viewer = viewer;
    this.overlay = null;
    this.handleClick = null;
    this.handleClickEndfun; //用于点击结束返回要素
}

/**
 * @description: 设置ol弹窗
 * @param {} dom
 * @param {fun} fun 点击结束返回要素
 * @return {*}
 */
Overlay.prototype.setOverlay = function (dom, fun) {
    if (fun) {
        this.handleClickEndfun = fun;
    }
    this.overlay = dom;

    // 创建一个箭头函数保留 this 上下文，并保存引用
    this.handleClick = (click) => {
        const pickedObject = this.viewer.scene.pick(click.position);
        if (Cesium.defined(pickedObject) && pickedObject.id) {
            this.handleClickEndfun(pickedObject);

            this.overlay.style.position = 'absolute';
            this.overlay.style.left = click.position.x + 'px';
            this.overlay.style.top = click.position.y + 'px';
            this.overlay.style.display = 'block';
        }
    };

    // 绑定事件
    let handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    handler.setInputAction(this.handleClick, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};

/**
 * @description: 移除地图点击事件
 * @return {void}
 */
Overlay.prototype.removeOverlay = function () {
    // 移除左键点击事件
    let handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
};

export default Overlay;
